package com.sara.dao;


import com.sara.entity.Movie;
import com.sara.util.DbHelp;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MovieDao {
    public int count(String keys) {
        String sql = "select count(*) from t_movie ";
        List<Object> paramsList = new ArrayList<>();
        if(StringUtils.isNotEmpty(keys)){
            keys = "%" + keys + "%";
            sql += "where movie_name like ?";
            paramsList.add(keys);

        }
        return DbHelp.query(sql, new ScalarHandler<Long>(), paramsList.toArray()).intValue();

    }

    public List<Movie> queryMovieByParams(Map<String,String> params) {
        String sql = "select * from t_movie ";
        List<Object> paramList = new ArrayList<>();
        if(StringUtils.isNotEmpty(params.get("keys"))){
            sql+="where movie_name like ?";
            String keys = params.get("keys");
            keys = "%" + keys + "%";
            paramList.add(keys);

        }
        sql += " limit ?,?";
        paramList.add(Integer.parseInt(params.get("start")));
        paramList.add(Integer.parseInt(params.get("pageSize")));
        return  DbHelp.query(sql,  new BeanListHandler<Movie>(Movie.class, new BasicRowProcessor(new GenerousBeanProcessor())),paramList.toArray());

    }
}
